package com.exam.serviceimpl;

import com.exam.entity.FillQuestion;
import com.exam.entity.JudgeQuestion;
import com.exam.entity.MultiQuestion;
import com.exam.entity.TrainingQuestionSet;
import com.exam.mapper.FillQuestionRepository;
import com.exam.mapper.JudgeQuestionRepository;
import com.exam.mapper.MultiQuestionRepository;
import com.exam.mapper.TrainingQuestionSetRepository;
import com.exam.service.TrainingQuestionSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TrainingQuestionSetServiceImpl implements TrainingQuestionSetService {

    @Autowired
    private FillQuestionRepository fillQuestionRepository;

    @Autowired
    private MultiQuestionRepository multiQuestionRepository;

    @Autowired
    private JudgeQuestionRepository judgeQuestionRepository;

    @Autowired
    private TrainingQuestionSetRepository trainingQuestionSetRepository;

    @Override
    public TrainingQuestionSet generateTrainingSet(String course, int fillQuestionCount, int multiQuestionCount, int judgeQuestionCount) {
        // 获取题目
        List<FillQuestion> fillQuestions = fillQuestionRepository.findAll().stream()
                .filter(q -> q.getSubject().equals(course))
                .limit(fillQuestionCount)
                .collect(Collectors.toList());

        List<MultiQuestion> multiQuestions = multiQuestionRepository.findAll().stream()
                .filter(q -> q.getSubject().equals(course))
                .limit(multiQuestionCount)
                .collect(Collectors.toList());

        List<JudgeQuestion> judgeQuestions = judgeQuestionRepository.findAll().stream()
                .filter(q -> q.getSubject().equals(course))
                .limit(judgeQuestionCount)
                .collect(Collectors.toList());

        // 创建训练题
        TrainingQuestionSet trainingSet = new TrainingQuestionSet();
        trainingSet.setName("Training Set for " + course);
        trainingSet.setCourse(course);
        trainingSet.setTotalQuestions(fillQuestions.size() + multiQuestions.size() + judgeQuestions.size());

        List<String> questionIds = new ArrayList<>();
        fillQuestions.forEach(q -> questionIds.add("F-" + q.getId()));
        multiQuestions.forEach(q -> questionIds.add("M-" + q.getId()));
        judgeQuestions.forEach(q -> questionIds.add("J-" + q.getId()));
        trainingSet.setQuestions(questionIds);

        return trainingQuestionSetRepository.save(trainingSet);
    }

    @Override
    public TrainingQuestionSet getTrainingSetById(Long id) {
        return trainingQuestionSetRepository.findById(id).orElse(null);
    }

    @Override
    public Page<TrainingQuestionSet> getAllTrainingSets(Pageable pageable) {
        return trainingQuestionSetRepository.findAll(pageable);
    }

    @Override
    public void deleteTrainingSet(Long id) {
        trainingQuestionSetRepository.deleteById(id);
    }
}